package sskj.regulatory.presentation.screen.signature.signedlist

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import com.qmuiteam.qmui.widget.QMUITabSegment
import kotlinx.android.synthetic.main.fragment_signed_list.view.*
import kotlinx.android.synthetic.main.fragment_signed_main.view.*
import sskj.regulatory.R
import sskj.regulatory.presentation.adapter.NormalPagerAdapter
import sskj.regulatory.presentation.base_mvp.base.BaseFragment
import sskj.regulatory.presentation.screen.register.RegisterFragment
import sskj.regulatory.presentation.screen.signature.signedmain.SignedMainPresenter
import javax.inject.Inject


class SignedMainFragment : BaseFragment<SignedMainContract.View, SignedMainContract.Presenter>(), SignedMainContract.View {


    @Inject
    lateinit var mPresenter: SignedMainPresenter

    private lateinit var mPagerAdapter: NormalPagerAdapter

    private val fragments: ArrayList<Fragment> = ArrayList<Fragment>();

    override fun initPresenter() = mPresenter

    override fun injectDependencies() {
        activityComponent.inject(this)
    }
    override fun onBackPressed(): Boolean = true

    override val layoutResId: Int = R.layout.fragment_signed_main

    override fun initView() {
        initTopBar()
        initTabAndPager()
    }

    private fun initTopBar() {

        mCacheView.sgTopBar.setTitle("电子签章");
        mCacheView.sgTopBar.addLeftBackImageButton().setOnClickListener { _ ->
            popBackStack()
        }

        mCacheView.sgTopBar.addRightTextButton("更多", R.id.topbar_right_more_button).setOnClickListener { _ -> }
    }

    private fun initTabAndPager() {

        fragments.add(SignedListFragment())
        fragments.add(SignedListFragment())

        mPagerAdapter = NormalPagerAdapter(childFragmentManager, fragments)

        mCacheView.sgViewPager.adapter = mPagerAdapter

        mCacheView.sgViewPager.setCurrentItem(0, false)

        mCacheView.sgTabSegment.addTab(QMUITabSegment.Tab("待签"))
        mCacheView.sgTabSegment.addTab(QMUITabSegment.Tab("已签"))

        mCacheView.sgTabSegment.setHasIndicator(true)
        mCacheView.sgTabSegment.setIndicatorPosition(false)
        mCacheView.sgTabSegment.setIndicatorWidthAdjustContent(true)

        mCacheView.sgTabSegment.setupWithViewPager(mCacheView.sgViewPager, false)

        mCacheView.sgTabSegment.mode = QMUITabSegment.MODE_FIXED

        mCacheView.sgTabSegment.addOnTabSelectedListener(object : QMUITabSegment.OnTabSelectedListener {

            override fun onTabSelected(index: Int) {

                mCacheView.sgTabSegment.hideSignCountView(index)
                mCacheView.sgViewPager.setCurrentItem(index, false)

            }

            override fun onTabUnselected(index: Int) {

            }

            override fun onTabReselected(index: Int) {
                mCacheView.sgTabSegment.hideSignCountView(index)

            }

            override fun onDoubleTap(index: Int) {

            }
        })

    }
}